package com.chuangke.admin.controller;

import com.chuangke.admin.entity.SysDict;
import com.chuangke.admin.entity.SysDictType;
import com.chuangke.admin.entity.SysUser;
import com.chuangke.admin.service.SysDictService;
import com.chuangke.admin.service.SysDictTypeService;
import com.chuangke.admin.service.SysUserService;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典控制器
 * @author chuangke
 * @date Oct 29, 2018
 */
@RestController
@RequestMapping("dict")
public class SysDictController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private SysDictTypeService sysDictTypeService;
	
	@PreAuthorize("hasAuthority('sys:dict:add') AND hasAuthority('sys:dict:edit')")
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody SysDict record) {
		sysDictService.save(record) ;
		return HttpResult.ok();
	}
	
	@PostMapping(value="/saveowndept")
	public HttpResult saveOwnDept(@RequestBody SysDict record) {
		SysUser sysUser = sysUserService.findById(SecurityUtils.getUserId()) ;
		record.setDeptId(sysUser.getDeptId());
		sysDictService.save(record) ;
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('sys:dict:delete')")
	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<SysDict> records) {
		sysDictService.delete(records) ;
		return HttpResult.ok();
	}

//	@PreAuthorize("hasAuthority('sys:dict:view')")
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody Map<String,Object> params) {
		return HttpResult.ok(sysDictService.findPage(new CkPageQuery(params)));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value="/findPageByColumns")
	public HttpResult findPageByColumns(@RequestBody Map<String,Object> params) {
		return HttpResult.ok(sysDictService.findPageByColumns(new CkPageQuery(params)));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value="/findowndeptPage")
	public HttpResult findOwnDeptPage(@RequestBody Map<String,Object> params) {
		SysUser sysUser = sysUserService.findById(SecurityUtils.getUserId()) ;
		params.put("deptId",sysUser.getDeptId()) ;
		return HttpResult.ok(sysDictService.findPage(new CkPageQuery(params)));
	}
	
//	@PreAuthorize("hasAuthority('sys:dict:view')")
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/findByLable")
	public HttpResult findByLable(@RequestParam String lable) {
		return HttpResult.ok(sysDictService.findByName(lable));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/find/{type}")
	public HttpResult findByType(@PathVariable("type") String type) {
		Map<String,SysDict> dictList = sysDictService.findMap(type) ;
		return HttpResult.ok(dictList.values());
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/finddeptdata/{type}")
	public HttpResult findDeptDataByType(@PathVariable("type") String type,@RequestParam String deptId) {
		Map<String,SysDict> dictList = sysDictService.findMap(type,deptId) ;
		return HttpResult.ok(dictList.values());
	}
	
	@PreAuthorize("hasAuthority('sys:dict:view')")
	@GetMapping(value="/findDictType")
	public HttpResult findDictType() {
		List<SysDictType> list = sysDictTypeService.findList();
		return HttpResult.ok(list);
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/findTreeByType")
	public HttpResult findTreeByType(@RequestParam String type,@RequestParam String label,@RequestParam String deptId) {
//		return HttpResult.ok(sysDictService.findTreeByType(type,label,deptId));
		return HttpResult.ok(sysDictService.findTree(type));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/findTreeWithView")
	public HttpResult findTreeWithView(@RequestParam String type,@RequestParam String label,@RequestParam String deptId) {
		if("null".equals(deptId)) {
			deptId = null ;
		}
		return HttpResult.ok(sysDictService.findTreeWithView(type,deptId,label));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value="/tree")
	public HttpResult tree(@RequestBody Map<String,Object> params) {
		String type = (String) params.get("type") ;
		String name = (String) params.get("name") ;
		String deptId = (String) params.get("deptId") ;
		if("null".equals(deptId)) {
			deptId = null ;
		}
		return HttpResult.ok(sysDictService.findTreeWithView(type,deptId,name));
	}
}
